
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;

using Swaf;
using Swaf.BizObj;
using Swaf.Config;
using Swaf.Security;

namespace Swaf.Versioning
{
	/// <summary>
	/// This class provides the base implementation of the VersioningPartner.  VersioningPartner works
	/// in conjuction with VersioningDictionaries to provide a context sensitive mapping of requested keys
	/// to versioned keys (i.e. DBCalcs --> DBCalcs::Signature::0401).
	/// </summary>
	public class BaseVersioningPartner : IConfigPartner 
	{
		public const string VERSION_DELIMITER = "::";

		protected IApplication m_app;
		protected bool m_IsConfigLoading=false;
		public BaseVersioningPartner(IApplication app)
		{
			m_app = app;
		}
		
		/// <summary>
		/// This method is called by the ConfigManager at the beginning of the loading of the Config file.
		/// </summary>
		/// <param name="config"></param>
		public void registerForConfig(IConfigManager config)
		{
			m_IsConfigLoading=true;
		}

		
		/// <summary>
		/// This method is called by the ConfigManager once the configuration file is completely loaded.
		/// </summary>
		public void checkConfigStatus()
		{
			m_IsConfigLoading=false;
		}
		
		/// <summary>
		/// This property returns true if the system is in the process of loading the configuration file.  The versioning dictionaries
		/// use this method to determine how to look to see if a value already exists or not.
		/// </summary>
		public bool IsConfigLoading
		{
			get
			{
				return m_IsConfigLoading;
			}
		}

		/// <summary>
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public virtual string resolve(string key)
		{
			if (CurrentUser == null)
				return key;
			return string.Format("{0}{1}{2}", key, VERSION_DELIMITER, ActiveCompany.get("shortName"));
		}


		/// <summary>
		/// This method uses context information combined with the base path passed in to
		/// build a search path.
		/// </summary>
		/// <param name="app"></param>
		/// <param name="basePath"></param>
		/// <returns></returns>
		public virtual string resolvePath(string basePath)
		{
			return basePath;
		}

		public virtual IList getVersionInfo(IApplication app, string key, string basePath, string callContext )
		{
			return null;
		}



		/// <summary>
		/// 
		/// </summary>
		protected static IApplication App { get { return Application.currentApp; } }
		/// <summary>
		/// 
		/// </summary>
		protected static ISecurityPrincipal CurrentPrincipal
		{
			get
			{
				Debug.Assert(App.securityMgr.currentPrincipal != null);
				return App.securityMgr.currentPrincipal;
			}
		}

		protected static IBizObj CurrentUser
		{
			get
			{
				Debug.Assert(App.securityMgr.currentPrincipal.additionalInfo != null);
				Debug.Assert(App.securityMgr.currentPrincipal.additionalInfo is IBizObj);
				return (IBizObj)CurrentPrincipal.additionalInfo;
			}
		}
		/// <summary>
		/// 
		/// </summary>
		protected static Swaf.BGuid.Guid CurrentCompanyId
		{
			get
			{
				Debug.Assert(CurrentUser.get("companyId") != null);
				Debug.Assert(CurrentUser.get("companyId") is Swaf.BGuid.Guid);
				return (Swaf.BGuid.Guid)CurrentUser.get("companyId");
			}
		}
		/// <summary>
		/// 
		/// </summary>
		protected static Swaf.BGuid.Guid ActiveCompanyId
		{
			get
			{
				Debug.Assert(CurrentUser.get("activeCompanyId") != null);
				Debug.Assert(CurrentUser.get("activeCompanyId") is Swaf.BGuid.Guid);
				return (Swaf.BGuid.Guid)CurrentUser.get("activeCompanyId");
			}
			set
			{
				CurrentUser.put("activeCompanyId", value);
			}
		}

		protected static IBizObj ActiveCompany
		{
			get
			{
				return App.ruleMgr.execute("getCachedCompanyInfo", ActiveCompanyId).CallResults as IBizObj;
			}
		}

	}
	public class VersionedCatalogSearchEntry
	{
		protected string m_versionString=null;
		protected string m_searchFileName=null;
		protected string m_versionPath=null;
		public VersionedCatalogSearchEntry(string key, string fileName, string path)
		{
			m_versionString=key;
			m_searchFileName=fileName;
			m_versionPath=path;
		}
		public VersionedCatalogSearchEntry()
		{
		}
		public string versionString
		{
			get{return m_versionString;}
			set{m_versionString=value;}
		}
		public string searchFileName
		{
			get{return m_searchFileName;}
			set{m_searchFileName=value;}
		}
		public string versionPath
		{
			get{return m_versionPath;}
			set{m_versionPath=value;}
		}
	}
}
